package MySingleLinkedList;

public class SingleLinkedList implements ISingleLinkedList{


    static class ListNode{
        public int val;
        public ListNode next;

        public ListNode(int val) {
            this.val=val;
        }
    }

    public ListNode head;

    public void createList() {
        ListNode node1=new ListNode(12);
        ListNode node2=new ListNode(23);
        ListNode node3=new ListNode(34);
        ListNode node4=new ListNode(45);
        ListNode node5=new ListNode(56);

        node1.next=node2;
        node2.next=node3;
        node3.next=node4;
        node4.next=node5;

        this.head=node1;
    }

    @Override
    public void display() {
        ListNode cur=head;
        while(cur!=null) {
            System.out.print(cur.val+" ");
            cur=cur.next;
        }
        System.out.println();
    }

    @Override
    public int size() {
        int count=0;
        ListNode cur=head;
        while(cur!=null) {
            count++;
            cur=cur.next;
        }
        return count;
    }

    @Override
    public void addFirst(int val) {
        ListNode node=new ListNode(val);
        if(head==null) {
            head=node;
            return;
        }
        node.next=head;
        head=node;
    }

    @Override
    public void addLast(int val) {
        ListNode node=new ListNode(val);
        ListNode cur=head;
        if(head==null) {
            head=node;
            return;
        }
        while(cur.next!=null) {
            cur=cur.next;
        }
        cur.next=node;
    }

    public void checkLegalOfIndex(int index) {
        if(index<0 || index>size()) {
            throw new IndexNotLegalException("Index位置不合法");
        }
    }

    @Override
    public void addIndex(int index, int val) throws IndexNotLegalException{
        try{
            checkLegalOfIndex(index);
        }catch(IndexNotLegalException e) {
            e.printStackTrace();
        }
        if(index==0) {
            addFirst(val);
            return;
        }
        else if(index==size()) {
            addLast(val);
            return;
        }
        ListNode node=new ListNode(val);
        ListNode cur=findIndexSubOne(index);
        node.next=cur.next;
        cur.next=node;
    }

    public ListNode findIndexSubOne(int index) {
        ListNode cur=head;
        for(int i=1;i<=index-1;i++) {
            cur=cur.next;
        }
        return cur;
    }

    @Override
    public boolean contains(int val) {
        ListNode cur=head;
        while(cur!=null) {
            if(cur.val==val) {
                return true;
            }
            cur=cur.next;
        }
        return false;
    }

    @Override
    public void remove(int val) {
        if(head==null) {
            return;
        }
        if(!contains(val)) {
            System.out.println("不存在该数");
            return;
        }
        if(head.val==val) {
            head=head.next;
            return;
        }
        ListNode cur=head;
        while(cur.next!=null) {
            if(cur.next.val==val) {
                cur.next=cur.next.next;
                return;
            }
            cur=cur.next;
        }
    }

    @Override
    public void removeAllKey(int val) {
        if(head==null) {
            return;
        }
        ListNode prev=head;
        ListNode cur=prev.next;
        while(cur!=null) {
            if(cur.val==val) {
                prev.next=cur.next;
//                cur=cur.next;
            }else {
                prev=cur;
//                cur=cur.next;
            }
            cur=cur.next;
        }
        if(head.val==val) {
            head=head.next;
        }
    }

    @Override
    public void clear() {
        head=null;
    }


    public boolean chkPalindrome( ) {
        // write code here
        if(head==null || head.next==null) {
            return true;
        }
        //1.找到链表的中间节点
        ListNode fast=head;
        ListNode slow=head;
        while(fast!=null && fast.next!=null) {
            fast=fast.next.next;
            slow=slow.next;
        }
        //2.翻转中间节点后面的节点
        ListNode cur=slow.next;
        while(cur!=null) {
            ListNode curN=cur.next;
            cur.next=slow;
            slow=cur;
            cur=curN;
        }
        //3.slow从后往前，fast从前往后，知道他俩相遇
        ListNode cur2=head;
        while(cur2!=slow) {
            if(cur2.val!=slow.val) {
                return false;
            }
            if(cur2.next==slow) {   //偶数情况下，当head的的下一个节点就是slow时就表示两者现在相遇
                return true;
            }
            cur2=cur2.next;
            slow=slow.next;
        }
        return true;
        //偶数节点的head和slow没法相遇
    }


    public boolean hasCycle(ListNode head) {
        ListNode fast=head;
        ListNode slow=head;
        while(fast!=null && fast.next!=null && fast.next.next!=null) {
            fast=fast.next.next.next;
            slow=slow.next.next;
            if(fast==slow) {
                return true;
            }
        }
        return false;
    }

    public ListNode detectCycle(ListNode head) {
        ListNode fast=head;
        ListNode slow=head;
        while(fast!=null && fast.next!=null) {
            fast=fast.next.next;
            slow=slow.next;
            if(slow==fast) {
                break;
            }
        }
        if(fast==null || fast.next==null) {
            return null;
        }
        //相遇了，fast从相遇点开始，slow从head开始，
        fast=head;
        while(fast!=slow) {
            fast=fast.next;
            slow=slow.next;
        }
        return fast;
    }

    public void createLoop() {
        ListNode cur=head;
        while(cur.next!=null) {
            cur=cur.next;
        }
        //cur指向尾结点
        cur.next=head.next;
    }
}
